package com.cat.yygh.cmn.controller;


import com.cat.yygh.cmn.service.DictService;
import com.cat.yygh.common.result.R;
import com.cat.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-11-05
 */
@Api("字典设置")
@RestController
@RequestMapping("/admin/cmn")
public class DictController {
    @Autowired
    private DictService dictService;
    @GetMapping("/getChildList/{pid}")
//    @Cacheable(value = "",key = "'selectIndexList'+#pid")
//    @Cacheable(value = "cmn",keyGenerator = "keyGenerator")
    public R getChildListByPid(@PathVariable Long pid){
        List<Dict> childList = dictService.getChildListByPid(pid);
        return R.ok().data("items",childList);
    }

    @ApiOperation(value = "上传字典信息")
    @PostMapping("/upload")
    public void upload(MultipartFile multipartFile) throws IOException {
        dictService.upload(multipartFile);
    }
    @ApiOperation("下载字典信息")
    @GetMapping("/download")
    @CacheEvict(value = "cmn",allEntries = true)
    public void download(HttpServletResponse response) throws IOException {
        dictService.download(response);
    }
    @GetMapping("/{value}}")    //提供给服务调用的必须加value
    public String getNameByValue(@PathVariable(value = "value")Long value){
        return dictService.getNameByValue(value);
    }
    @GetMapping("/{dictCode}/{value}")
    public String getNameByDictCodeAndValue(@PathVariable(value = "dictCode")String dictCode,
                                          @PathVariable(value = "value")Long value){
        return dictService.getNameByDictCodeAndValue(dictCode,value);
    }
    @ApiOperation("根据代码查询子元素")
    @GetMapping("/dict/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){
        List<Dict> list =dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }
}

